//
//  WelcomeViewController.swift
//  weiboxiangmu
//
//  Created by wei_jingyun on 15/6/30.
//  Copyright © 2015年 wei_jingyun. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    /// 头像的底部约束
    var bottomConstraint: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 加载图片
        if let urlString = sharedUserAccount?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: urlString)!)
        }
    }

    // 显示完成执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        bottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 180)
        
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10 , options: UIViewAnimationOptions.AllowAnimatedContent, animations: { () -> Void in
             self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1.0, animations: { () -> Void in
                    self.messageLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        // true 表示显示 MainViewController
                     NSNotificationCenter.defaultCenter().postNotificationName(JYSwitchRootVCNotification, object: true)
                })
                
        }
    }
    
    
    // MARK: - 设置界面控件
    override func loadView() {
        view = UIView()
        // 添加控件
        let backImageView = UIImageView(image: UIImage(named: "ad_background"))
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        // 自动布局设置
        backImageView.ff_edgesView(UIedgeView().more(tlbr: ff_tlbr.all, v: view))
        
        let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90 , height: 90) , offset: CGPoint(x: 0, y: -160))
        
         bottomConstraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
         messageLabel.ff_AlignHorizontal(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 8))
    }

    lazy var iconView : UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        return iv
    }()
    
    lazy var messageLabel : UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.sizeToFit()
        label.alpha = 0
        return label
    }()

}
